package com.cwt.service.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cwt.common.constant.SysConstant;
import com.cwt.common.result.AjaxResult;
import com.cwt.common.utils.FileUtils;
import com.cwt.service.domain.SysNotice;
import com.cwt.service.service.SysNoticeService;
import com.cwt.service.mapper.SysNoticeMapper;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
* @author Acer
* @description 针对表【tb_sys_notice(系统公告表)】的数据库操作Service实现
* @createDate 2023-08-29 17:10:42
*/
@Service
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeMapper, SysNotice>
    implements SysNoticeService{

    @Override
    public AjaxResult saveNoticeImg(MultipartFile file) {
        File img = FileUtils.createFile(file, SysConstant.SYS_NOTICE_DIR + file.getOriginalFilename());

        //首先，因为这儿是非Controller，所以，通过RequestContextHolder获取HttpServletRequest；
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert attributes != null;
        HttpServletRequest request = attributes.getRequest();
        //然后，拼凑订单支付url的一部分：“127.0.0.1:8083”;
        String address = "localhost" + ":" + request.getLocalPort() + request.getContextPath();
        return AjaxResult.success("http://" + address + "/noticeImg/" + img.getName());
    }
}




